import React, { Component } from "react";
// ant
import { Modal, message } from "antd";
// API
import { departmentAdd, getDepartmentDetail, updateDepartment } from "@/api/raycloud";
// 组件
import FormCom from "@c/form/Index";


class DepartmentModal extends Component {
    constructor(props){
        super(props);
        this.state = {
            isModalVisible: false,
            department_id: "",
            formConfig: {
                url: "departmentAdd",
                editKey: "",
                initValue: {
                    number: 0,
                    status: "",
                    parentId: ""
                },
                setFieldValue: {},
                formatFormKey: "id"
            },
            formLayout: {
                labelCol: { span: 4 },
                wrapperCol: { span: 20 }
            },
            formItem: [
                { 
                    type: "Input",
                    label: "部门名称", 
                    name: "name", 
                    required: true, 
                    style: { width: "200px" },
                    placeholder: "请输入部门名称"
                },
                { 
                    type: "Input",
                    label: "简称", 
                    name: "code", 
                    required: true, 
                    style: { width: "200px" },
                    placeholder: "请输入部门简称"
                },
                { 
                    type: "SelectComponent",
                    mode: "multiple",
                    label: "关联账号", 
                    url: "accountList",
                    method: "get",
                    name: "account_list",
                    propsKey: {
                        label: "name",
                        value: "name"
                    },
                    required: true,
                    placeholder: "请选择"
                },
                { 
                    type: "SelectComponent",
                    mode: "multiple",
                    label: "管理员", 
                    url: "userList",
                    method: "get",
                    name: "admin_list",
                    propsKey: {
                        label: "realname",
                        value: "realname"
                    },
                    required: true,
                    placeholder: "请选择"
                },
            ]
        };
    }

    componentDidMount(){
        this.props.onRef(this);
    }

    onFormRef = (ref) => {
        this.child = ref;
    }

    // 关闭弹窗
    handleCancel = () => {
        // 清单表单
        this.child.onReset();
        this.setState({
            isModalVisible: false,  // 关闭弹窗
            formConfig: { setFieldValue: "" },  // 清除form的初始数据
        })
    }
    
    /** 弹窗 */
    visibleModal = (params) => {
        this.setState({
            isModalVisible: params.status,
            department_id: params.department_id
        }, () => {
            this.getDetailed()
        })
    }

    /** department详情 */
    getDetailed = () => {
        if(!this.state.department_id) { return false; }
        getDepartmentDetail({id: this.state.department_id}).then(response => {
            const data = response.data.data.items[0];
            console.log(data)
            this.setState({
                formConfig: {
                    setFieldValue: data
                }
            })
        })
    }

    onBlurEvent = (e) => {
        const value = e.currentTarget.value;
        if(value) {
            this.updateItem(value ? false : true);
            return false;
        }
    }
    // 提交按钮
    submit = (value) => {
        this.state.department_id ? this.handlerFormEdit(value) : this.handlerFormAdd(value);
        
    }
    // 添加 提交表单
    handlerFormAdd= (value) => {
        const requestData = value;
        console.log(requestData)
        departmentAdd(requestData).then(response => {
            const responseData = response.data;
            // 提示
            message.info(responseData.msg);
            // 关闭弹准备
            this.handleCancel(false);
        })
    }
    // 编辑 提交表单
    handlerFormEdit= (value) => {
        console.log(value)
        const requestData = value;
        const id = this.state.department_id;
        updateDepartment(id, requestData).then(response => {
            const responseData = response.data;
            // 提示
            message.info(responseData.msg);
            // 关闭弹准备
            this.handleCancel(false);
        })
    }

    render(){
        console.log(this.props)
        return (
            <Modal title="事业部信息" visible={this.state.isModalVisible} footer={null} onCancel={this.handleCancel}>
                <FormCom 
                    onRef={this.onFormRef} 
                    onBlur={this.onBlurEvent} 
                    formItem={this.state.formItem} 
                    formLayout={this.state.formLayout} 
                    formConfig={this.state.formConfig} 
                    submit={this.submit}
                >
                </FormCom>
            </Modal>
        )
    }
}

export default DepartmentModal;